package com.bw.common.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.room.RoomDatabase
import com.bw.database.db.AppDataBase
import com.bw.database.db.RoomUtils
import com.bw.http.Res
import com.bw.http.RetrofitUtils
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.launch
import java.lang.reflect.ParameterizedType

/**
 * 基础ViewModel
 */
abstract class BaseViewModel<API : BaseApiService>:ViewModel() {


    /**
     * 协程中如何处理异常
     * 1.try-catch
     * 2.自定义CoroutineExceptionHandLer（协程异常处理类）
     * 3.创建独立作用域 SupervisorJob 异常对其他协程没影响
     */


    private val type by lazy { (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments }
    protected val apiService: API by lazy {
        val clazz = type[0] as Class<API>
        RetrofitUtils.createService(clazz)
    }
    protected val database:AppDataBase = RoomUtils.getDataBase()




}
